<template>
  <div>
      <a-form-model 
      ref="ha_taskForm"
      :rules="rules"
      :model="ha_task">
              <a-form-model-item
              :label-col="labelCol"
              :wrapper-col="wrapperCol"
              label="任务名"
              prop="name"
              has-feedback>
                        <a-input
                        v-if="!isView"
                        allow-clear
                        v-model="ha_task.name"
                        :maxLength="100"
                        placeholder="请输入任务名" />
                        <span v-text="ha_task.name" v-else></span>
              </a-form-model-item>
              <a-form-model-item
              :label-col="labelCol"
              :wrapper-col="wrapperCol"
              label="任务类"
              prop="classname"
              has-feedback>
                        <a-input
                        v-if="!isView"
                        allow-clear
                        v-model="ha_task.classname"
                        :maxLength="100"
                        placeholder="请输入任务类" />
                        <span v-text="ha_task.classname" v-else></span>
              </a-form-model-item>
              <a-form-model-item
              :label-col="labelCol"
              :wrapper-col="wrapperCol"
              label="cron表达式"
              prop="cron"
              
              has-feedback>
                        <a-input
                        v-if="!isView"
                        allow-clear
                        v-model="ha_task.cron"
                        placeholder="请输入cron表达式"/>
                        <span v-text="ha_task.cron" v-else></span>
              </a-form-model-item>
      <a-form-model-item>
      <div style="text-align: center;">
          <a-input
          type="hidden"
          :disabled="true"
          v-model="ha_task.id"
          />
        <a-button :style="{ marginRight: '8px' }"  icon="close" @click="onClose">
          关闭
        </a-button>
        <a-button :style="{ marginRight: '8px'}" icon="reload" @click="resetForm" v-if="!isView">
          重置
        </a-button>
        <a-button type="primary" @click="onSubmit"  icon="save" :loading="loading" v-if="!isView">
          保存
        </a-button>
      </div>
     </a-form-model-item>
    </a-form-model >
  </div>
</template>
<script>
/* eslint-disable */
import { saveHa_task , viewHa_task} from '@/api/base/ha_task'
// 没用到枚举请删除
import { queryEnumerationValuesByName } from '@/api/base/enumeration'
// 没用到附件请删除
import { minioUpload } from '@/api/base/file'
// 没用到附件请删除
function getBase64(file) {
  return new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = () => resolve(reader.result);
    reader.onerror = error => reject(error);
  });
}
export default {
  data () {
    return {
      labelCol: { span: 6 }, // label宽
      wrapperCol: { span: 18 }, // 组件宽
      loading: false, //按钮显示
      isView: false, //是否查看
      ha_task: {
          id: '',
          cron: '',
          executing: '0',
          name: '',
          classname: '',
          executeip: '',
          executetime: '',
          enable: '0',
      }, //调度任务信息
      rules: {
        name: [{ required: true, message: '请填写任务名称', trigger: 'blur' }],
        classname: [{ required: true, message: '请填写任务类路径', trigger: 'blur' }],
        cron: [{ required: true, message: '请填写cron表达式', trigger: 'blur' }]
      },
    }
  },
  created () {
  },
  methods: {
    /**
     * 调用父组件的方法收起抽屉
     */
    onClose () {
      this.$emit('close')
    },
    /**
     * 提供给父组件调用
     * 点击新增进入时重置表单数据
     */
    resetHa_task () {
      this.isView = false
      this.resetForm()
    },
    /**
     * 提供给父组件调用
     * 查询用户信息
     */
    findHa_taskById (id,isView) {
      this.isView = isView
      this.resetForm()

      viewHa_task(id).then(res => {
        this.ha_task = res.data
    	/**
		 *  枚举如果不能显示中文打开下面的代码
      	*/
      })
    },
    /**
     * 重置表单
     */
    resetForm() {
      this.$refs.ha_taskForm.resetFields();
    },
    /**
     * 提交表单
     */
    onSubmit() {
      this.$refs.ha_taskForm.validate(valid => {
      this.loading=true
        if (valid) {
          delete this.ha_task.primaryKey
          console.log('submit!', this.ha_task)
          const savem = this.$message.loading('数据正在保存中...', 0)
          saveHa_task(this.ha_task).then(res =>{
            if (res.code !== 0) {
              setTimeout(savem, 0)
              this.$message.error('调度任务保存失败')
              this.loading=false
            } else {
              setTimeout(savem, 0)
              this.$message.success('保存成功')
              this.$emit('reloadList')
              this.$emit('close')
              this.loading=false
            }
          })
        } else {
          this.loading=false
          // 加延时器的目的是为了vue实例更新未完成，我们可能获取DOM失败的问题
		  // 利用原生js的方式，获取所有失败的类名，获得一个数组，取第一个元素获取焦点
          setTimeout(()=>{
            const isError= document.getElementsByClassName("has-error");
            const input = isError[0].querySelector('input');
            //console.log(input)
            if(input !== null && input !== 'undefind' && input !== ''){
              isError[0].querySelector('input').focus() ? isError[0].querySelector('input').focus() : isError[0].querySelector('input').click()
            }else{
              isError[0].querySelector('textarea').focus()
            }
          },10)
          return false;
        }
      });
    }
  }
}
</script>
<style lang="less" src="@/style/base.less">

</style>